package com.monster.servlet.home;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.monster.pojo.Song;
import com.monster.service.SongService;
import com.monster.service.SongServiceImpl;
import com.monster.util.SongUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

@WebServlet("/user/download")
public class DownloadServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ArrayList<Song> songs = new ArrayList<>();
        SongService ss = new SongServiceImpl();
        String JArray = request.getParameter("data");
        ArrayList<Long> songIds = SongUtil.parseJSONtoSongId(JArray, "mid");
        for (int i = 0 ; i < songIds.size(); i++) {
            Song song = ss.getSongById(songIds.get(i));
            songs.add(song);
        }
        PrintWriter out = response.getWriter();
        out.print(JSONArray.toJSON(songs));
        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
